/**
 * 
 */
package it.polimi.traveldream.usermanagement;

import java.util.ArrayList;
import java.util.List;

import it.polimi.traveldream.consistency.PackageConsistencyManager;
import it.polimi.traveldream.daos.PackageManager;
import it.polimi.traveldream.dtos.DefaultPackageDTO;
import it.polimi.traveldream.exception.ConsistencyException;
import it.polimi.traveldream.exception.NoIdException;
import it.polimi.traveldream.persistence.DefaultPackage;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import javax.annotation.security.RolesAllowed;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

@Stateless
public class PackageManagerBean implements PackageManager {

	@PersistenceContext
	private EntityManager em;

	@Resource
	private EJBContext context;

	private PackageConsistencyManager pcm;

	@PostConstruct
	private void init() {
		pcm = new PackageConsistencyManager(this);
	}

	@RolesAllowed({ Group._ADMIN })
	@Override
	public void addDefaultPackage(DefaultPackageDTO defaultPackageDTO)
			throws ConsistencyException {

		print("addDefaultPackage: " + defaultPackageDTO.toString());

		pcm.newPackageConsistency(defaultPackageDTO);

		DefaultPackage newDefaultPackage = new DefaultPackage(defaultPackageDTO);

		em.persist(newDefaultPackage);

	}

	@RolesAllowed({ Group._ADMIN })
	@Override
	public void updateDefaultPackage(DefaultPackageDTO defaultPackageDTO)
			throws ConsistencyException, NoIdException {

		print("updateDefaultPackage: " + defaultPackageDTO.toString());

		if (((Integer) defaultPackageDTO.getId()) == null) {
			throw new NoIdException();
		}

		pcm.newPackageConsistency(defaultPackageDTO);

		DefaultPackage defaultPackage = new DefaultPackage(defaultPackageDTO);
		defaultPackage.setId(defaultPackageDTO.getId());

		em.merge(defaultPackage);

	}

	@RolesAllowed({ Group._ADMIN })
	@Override
	public void removeDefaultPackage(DefaultPackageDTO defaultPackageDTO)
			throws NoIdException {

		print("removeDefaultPackage: " + defaultPackageDTO.toString());

		if (((Integer) defaultPackageDTO.getId()) == null) {
			throw new NoIdException();
		}

		em.remove(em.find(DefaultPackage.class, defaultPackageDTO.getId()));

	}

	@RolesAllowed({ Group._ADMIN })
	@Override
	public void removeDefaultPackagebyId(int id) {

		print("removeDefaultPackagebyId: " + ((Integer) id).toString());

		em.remove(em.find(DefaultPackage.class, id));

	}

	@Override
	public List<DefaultPackageDTO> getAll() {

		print("getAll");

		return convertToListDTO(em.createNamedQuery(DefaultPackage.FIND_ALL,
				DefaultPackage.class).getResultList());
	}

	@Override
	public List<DefaultPackageDTO> findByName(String name) {

		print("getAll: " + name);

		return convertToListDTO(em.createNamedQuery(DefaultPackage.FIND_BY_NAME).setParameter("name", name).getResultList());
	}

	@Override
	public DefaultPackageDTO findById(int defaultPackageId) {

		print("findById: " + ((Integer) defaultPackageId).toString());

		return convertToDTO(em.find(DefaultPackage.class, defaultPackageId));
	}

	/*
	 * Helper Method
	 */

	private List<DefaultPackageDTO> convertToListDTO(
			List<DefaultPackage> defaultPackages) {

		List<DefaultPackageDTO> defaultPackageDTOs = new ArrayList<DefaultPackageDTO>();

		for (int i = 0; i < defaultPackages.size(); i++) {
			defaultPackageDTOs.add(convertToDTO(defaultPackages.get(i)));
		}

		return defaultPackageDTOs;
	}

	private DefaultPackageDTO convertToDTO(DefaultPackage defaultPackage) {

		if (defaultPackage != null) {
			em.refresh(defaultPackage);
		}

		DefaultPackageDTO defaultPackageDTO = new DefaultPackageDTO();

		defaultPackageDTO.setId(defaultPackage.getId());
		defaultPackageDTO.setName(defaultPackage.getName());
		defaultPackageDTO.setDescription(defaultPackage.getDescription());
		defaultPackageDTO.setStartDate(defaultPackage.getStartDate());
		defaultPackageDTO.setEndDate(defaultPackage.getEndDate());

		return defaultPackageDTO;

	}

	private void print(String message) {
		System.out.print("PackageManagerBean: " + message);
	}
}
